package com.example.casper.Experiment2024.fragment;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

import com.blankj.utilcode.util.ThreadUtils;
import com.example.casper.Experiment2024.R;
import com.example.casper.Experiment2024.data.ShopDownLoader;
import com.example.casper.Experiment2024.data.ShopLocation;
import com.tencent.tencentmap.mapsdk.maps.CameraUpdateFactory;
import com.tencent.tencentmap.mapsdk.maps.TencentMap;
import com.tencent.tencentmap.mapsdk.maps.TextureMapView;
import com.tencent.tencentmap.mapsdk.maps.model.LatLng;
import com.tencent.tencentmap.mapsdk.maps.model.Marker;
import com.tencent.tencentmap.mapsdk.maps.model.MarkerOptions;

import java.util.ArrayList;

public class TencentMapFragment extends Fragment {

    private TextureMapView mapView;
    private TencentMap tencentMap;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tencent_map, container, false);
        mapView = view.findViewById(R.id.mapView);

        // 从json中拿数据信息
        new ShopDownloadTask().execute("https://file.notion.so/f/f/3500cdb4-2af2-4803-b797-b49933980410/8332c677-d2bd-461f-97da-5a84839038f4/shop.json?table=block&id=143148a1-26c1-8090-acdf-d425c3687691&spaceId=3500cdb4-2af2-4803-b797-b49933980410&expirationTimestamp=1732154400000&signature=duVfrPyopUxvf2OGULRFdkvmzDrQe0ZBx0bByS4hcqo&downloadName=shop.json");
        tencentMap = mapView.getMap();
        // 中心定位在暨南大学珠海校区
        LatLng point1 = new LatLng(22.255453, 113.54145);
        tencentMap.moveCamera(CameraUpdateFactory.newLatLng(point1));
//        // 创建一个Marker对象
//        MarkerOptions markerOptions = new MarkerOptions(point1).title("我是暨南大学珠海校区");
//        // 添加标记到地图上
//        tencentMap.addMarker(markerOptions);

        new Thread(new Runnable() {
            @Override
            public void run() {
                String responseData = new ShopDownLoader().download("https://file.notion.so/f/f/3500cdb4-2af2-4803-b797-b49933980410/8332c677-d2bd-461f-97da-5a84839038f4/shop.json?table=block&id=143148a1-26c1-8090-acdf-d425c3687691&spaceId=3500cdb4-2af2-4803-b797-b49933980410&expirationTimestamp=1732154400000&signature=duVfrPyopUxvf2OGULRFdkvmzDrQe0ZBx0bByS4hcqo&downloadName=shop.json");
                ArrayList<ShopLocation> shopLocations = new ShopDownLoader().parseJsonObjects(responseData);
                ThreadUtils.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        TencentMap tencentMap = mapView.getMap();
                        for (ShopLocation shopLocation : shopLocations) {
                            LatLng point1 = new LatLng(shopLocation.getLatitude(), shopLocation.getLongitude());
                            MarkerOptions markerOptions = new MarkerOptions(point1).title(shopLocation.getName());
                            Marker marker = tencentMap.addMarker(markerOptions);
                        }
                    }
                });
            }
        }).start();
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        mapView.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
        mapView.onStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    public class ShopDownloadTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
            return new ShopDownLoader().download(urls[0]);
        }

        @Override
        protected void onPostExecute(String responseData) {
            super.onPostExecute(responseData);
            if (responseData != null) {
                ArrayList<ShopLocation> shopLocations = new ShopDownLoader().parseJsonObjects(responseData);
                TencentMap tencentMap = mapView.getMap();
                for (ShopLocation shopLocation : shopLocations) {
                    LatLng point1 = new LatLng(shopLocation.getLatitude(), shopLocation.getLongitude());
                    MarkerOptions markerOptions = new MarkerOptions(point1).title(shopLocation.getName());
                    Marker marker = tencentMap.addMarker(markerOptions);
                }
            }
        }
    }
}